package com.ws.q1.controller;
import com.ws.q1.bean.GongYingShang;
import com.ws.q1.service.GongYingShangService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.SQLException;
import java.util.List;

@Controller
@RequestMapping("/shang")
public class GongYingShangController {
    @Autowired
    private GongYingShangService gongYingShangService;


    @RequestMapping("/add")
    public String add(GongYingShang gongYingShang) {
        int add = gongYingShangService.add(gongYingShang);
        if (add > 0) {
            return "redirect:/shang/list";
        } else {
            return "redirect:/shang/list";
        }
    }

    @RequestMapping("/list")
    public String list(Model model) throws SQLException {
        List<GongYingShang> list = gongYingShangService.list();
        model.addAttribute("list", list);
        return "gongyingshang/list";
    }


    @RequestMapping("/delete")
    public String delete(Integer id) {
        int result = gongYingShangService.deleteById(id);
        return "redirect:/shang/list";
    }

    @RequestMapping("/update")
    public String update(GongYingShang gongYingShang) {
        int id = gongYingShang.getId();
        gongYingShangService.update(id,gongYingShang);
        return "redirect:/shang/list";
    }

    @RequestMapping("/huixian")
    public String huixian(Integer id, Model model) {
        System.out.println(id);
        GongYingShang huixian = gongYingShangService.huixian(id);
        System.out.println(huixian);
        model.addAttribute("huixian", huixian);
        return "gongyingshang/update";
    }


    @RequestMapping("/tiao")
    public String tiao() {
        return "gongyingshang/add";
    }
}

